package ua.maxicash.dao;

import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import ua.maxicash.vo.JobType;
import ua.maxicash.vo.Worker;

import java.util.List;


@Repository("workerDao")
public class WorkerDaoImpl implements WorkerDao {

    private static final Logger logger = Logger.getLogger(WorkerDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Override
    @Transactional
    public void addWorker(Worker worker) {

        hibernateTemplate.saveOrUpdate(worker);
        logger.info("new worker has been successfully added : " + worker.toString());

    }

    @Override
    @Transactional
    public void updateWorker(Worker worker) {

        Worker aWorker = getWorkerById(worker.getIdUser());
        aWorker.setDateOfBirth(worker.getDateOfBirth());
        aWorker.setSex(worker.getSex());
        aWorker.setJobTypes(worker.getJobTypes());
        aWorker.setAbout(worker.getAbout());
        aWorker.setAvatar(worker.getAvatar());
        aWorker.setCity(worker.getCity());
        aWorker.setFirstName(worker.getFirstName());
        aWorker.setLastName(worker.getLastName());
        aWorker.setPhone(worker.getPhone());
        aWorker.setIcq(worker.getIcq());
        aWorker.setSkype(worker.getSkype());
        //todo always true
        aWorker.setEnabled(true);
        aWorker.setScheduleList(worker.getScheduleList());

        hibernateTemplate.merge(aWorker);

    }

    @Override
    @Transactional
    public void saveOrUpdateWorker(Worker worker) {

        hibernateTemplate.merge(worker);

    }


    @Override
    public List<Worker> listWorker() {
        //Session session = sessionFactory.openSession();
//        //Criteria criteria = session.createCriteria(Worker.class);
        DetachedCriteria crit = DetachedCriteria.forClass(Worker.class);
        crit.setFetchMode("scheduleList", FetchMode.SELECT);
        List<Worker> list = hibernateTemplate.findByCriteria(crit);
//        List<Worker> list = hibernateTemplate.find("from Worker");
        //   List<Worker> list = criteria.list();
        //session.close();


        return list;
    }

    @Override
    public Worker getWorkerByEmail(String email) {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Worker.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Worker worker = (Worker) criteria.uniqueResult();

        session.clear();
        session.close();

        return worker;
    }

    @Override
    public boolean emailIsFree(String email) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Worker.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Worker worker = (Worker) criteria.uniqueResult();
        session.clear();
        session.close();


        return worker == null;
    }

    @Override
    @Transactional
    public Worker updatePassword(String email, String password) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Worker.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Worker worker = (Worker) criteria.uniqueResult();
        worker.setPassword(password);
        session.saveOrUpdate(worker);
        session.close();
        return worker;
    }

    @Override
    public void saveWorkerWithJobType(Worker worker, JobType jobType) {
        Session session = sessionFactory.getCurrentSession();

        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(worker);
        tx.commit();
        session.close();
    }

    @Override
    public Worker getWorkerById(Long idWorker) {
        DetachedCriteria crit = DetachedCriteria.forClass(Worker.class);
        crit.add(Restrictions.idEq(idWorker));
        return (Worker)hibernateTemplate.findByCriteria(crit).get(0);
    }
}
